#!/usr/bin/env python3

import Hobot.GPIO as GPIO
import time

class LED_Controller:
    def __init__(self, pin, initial=GPIO.LOW, mode=GPIO.BOARD):
        """
        初始化 GPIO 控制器。

        :param pin: 使用的引脚编号。
        :param initial: 初始电平状态，默认为低电平。
        :param mode: 引脚编号模式，默认为 BOARD 模式。
        """
        self.pin = pin
        self.mode = mode
        self.state = initial
        GPIO.setmode(self.mode)
        GPIO.setup(self.pin, GPIO.OUT, initial=self.state)

    def set_high(self):
        """将引脚设置为高电平。"""
        GPIO.output(self.pin, GPIO.HIGH)
        self.state = GPIO.HIGH

    def set_low(self):
        """将引脚设置为低电平。"""
        GPIO.output(self.pin, GPIO.LOW)
        self.state = GPIO.LOW

    def toggle(self):
        """切换引脚的电平状态。"""
        self.state = GPIO.LOW if self.state == GPIO.HIGH else GPIO.HIGH
        GPIO.output(self.pin, self.state)

    def cleanup(self):
        """清理 GPIO 设置。"""
        GPIO.cleanup(self.pin)



if __name__ == '__main__':
    output_pin = 12  # BOARD 编码对应的引脚编号
    controller = LED_Controller(pin=output_pin, initial=GPIO.HIGH)

    print("Starting GPIO control. Press CTRL+C to exit.")
    try:
        while True:
            time.sleep(1)
            controller.toggle()
    except KeyboardInterrupt:
        print("Exiting program.")
    finally:
        controller.cleanup()
